package cn.edu.sjtu.tooling.controller;

import cn.edu.sjtu.tooling.domain.Task;
import cn.edu.sjtu.tooling.dto.TaskDTO;
import cn.edu.sjtu.tooling.enums.ResultEnum;
import cn.edu.sjtu.tooling.service.ToolingService;
import cn.edu.sjtu.tooling.util.ResultUtil;
import cn.edu.sjtu.tooling.vo.ResultVO;
import cn.edu.sjtu.tooling.vo.TasksWithTypeVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class ToolingController {

  private final ToolingService toolingService;

  public ToolingController(ToolingService toolingService) {
    this.toolingService = toolingService;
  }

  @GetMapping(
      value = "/api/state",
      params = {"processId", "taskId"})
  public ResultVO getStatus(String processId, String taskId) {
    return ResultUtil.success(toolingService.queryTaskState(processId, taskId));
  }

  @GetMapping(value = "/api/state")
  public ResultVO getStatus() {
    return ResultUtil.success(toolingService.queryECNState());
  }

  /*加入待处理队列*/
  @PostMapping(value = "/api/offer")
  public ResultVO addTaskToQueue(@RequestBody TaskDTO taskDTO) {
    boolean ret = toolingService.addTaskToQueue(taskDTO);
    if (ret) {
      return ResultUtil.success();
    }
    return ResultUtil.error(ResultEnum.TASK_OFFER_ERROR);
  }

  @GetMapping("/api/tasks")
  public ResultVO listTaskQueue() {
    //    List<TaskVO> taskVOQueue = flawDetectionService.listTaskQueue();
    //    return ResultUtil.success(taskVOQueue);
    Map<Integer, List<Task>> map = toolingService.retTasksPool();
    return ResultUtil.success(TasksWithTypeVO.build(map));
  }
}
